package com.smsc.headend.connector.netty.decoder;

import com.smsc.headend.connector.service.LogTerminalAccessService;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class BytesInboundCounter extends ByteToMessageDecoder {

    @Autowired
    LogTerminalAccessService logTerminalAccessService;

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        int bytesNumber = byteBuf.readableBytes();
        logTerminalAccessService.saveBytesCount(channelHandlerContext.channel(), bytesNumber, true);
        list.add(byteBuf.readBytes(byteBuf.readableBytes()));
    }
}
